<?php

namespace app\agent\controller;

use app\common\controller\Agent;
use app\common\library\Auth;
use fast\Random;
use think\Db;
use think\Exception;
use think\Validate;

/**
 * 商家管理
 *
 * @icon fa fa-user
 */
class ShopGoods extends Agent
{

    protected $relationSearch = true;
    protected $searchFields = 'goods.goods_name,shop_goods.shop_id';
    protected $multiFields = 'status';

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('ShopGoods');

    }

    /**
     * 查看
     */
    public function index($ids=null)
    {
        $this->view->assign("shop_id", $ids);
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->where($where)->with('goods','goodsImages')->order($sort, $order)->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        $this->assignconfig('shop_id',$ids);
        return $this->view->fetch();
    }

    /**
     * 商品新增
     */
    public function add($shop_id=null)
    {

        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            if ($params) {
                Db::startTrans();
                try {
                    $info = \app\common\model\ShopGoods::get(['goods_id'=>$params['goods_id'],'shop_id'=>$params['shop_id'],'is_del'=>0]);
                    if ($info)
                        exception('该商品在所选店铺中已经添加过，请勿重复添加');
                    //查找店铺商品中sort值最大的
                    $last_goods = \app\common\model\ShopGoods::where(['shop_id'=>$params['shop_id']])->order('sort desc')->find();
                    $params['sort'] = empty($last_goods) ? 0 : $last_goods['sort']+1;

                    $result = $this->model->validate('ShopGoods.add')->save($params);
                    if ($result === false) {
                        exception($this->model->getError());
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $goods_id = model('ShopGoods')->where('shop_id',$shop_id)->column('goods_id');
        $goods_list = model('Goods')->where('goods_status', 1)->whereNotIn('id',$goods_id)->order('goods_name', 'asc')->select();
        $this->view->assign("goods_list", $goods_list);
        $this->view->assign("shop_id", $shop_id);
        return $this->view->fetch();
    }


}
